package org.ssi.web.tags.bean;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.ssi.web.tags.Globals;

public class CounterTag extends TagSupport  {
	private static final long serialVersionUID = -6046921307830408944L;

	private boolean write = false;

	public boolean getWrite() {
		return write;
	}

	public void setWrite(boolean write) {
		this.write = write;
	}

	public CounterTag() {
	}

	public int doStartTag() throws JspException {
		int counter = 1;
		if (pageContext.getAttribute(Globals.COUNTER_KEY) != null) {
			counter = ((Integer) pageContext.getAttribute(Globals.COUNTER_KEY)).intValue();
			counter++;
		}
		pageContext.setAttribute(Globals.COUNTER_KEY, new Integer(counter));
		if (write) {
			try{
			pageContext.getOut().print(String.valueOf(counter));
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		return SKIP_BODY;
	}

	public void release() {
		super.release();
		write = false;
	}


}
